package com.akw.mq.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class MyMessageListener {
    public static final String DEAD_EXCHANGE_DIRECT = "exchange.dead.letter.vide";
    public static final String NORMAL_EXCHANGE_DIRECT = "exchange.normal.vide";
    public static final String DEAD_ROUTING_KEY = "routing.key.dead.letter.vide";
    public static final String NORMAL_ROUTING_KEY = "routing.key.normal.vide";
    public static final String DEAD_QUEUE_NAME = "queue.dead.letter.video";
    public static final String NORMAL_QUEUE_NAME = "queue.normal.vide";
    public static final String EXCHANGE_DIRECT = "exchange.direct.order";
    public static final String EXCHANGE_TIMEOUT = "exchange.test.timeout";
    public static final String ROUTING_KEY = "order";
    public static final String TIMEOUT_ROUTING_KEY = "routing.key.test.timeout";
    public static final String QUEUE_NAME = "queue.order";
    public static final String TIMEOUT_QUEUE_NAME = "queue.test.timeout";

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = QUEUE_NAME, durable = "true"),
//            exchange = @Exchange(value = EXCHANGE_DIRECT),
//            key = {ROUTING_KEY}
//    ))
    public void processMessage(String dataString, Message message, Channel channel) throws IOException {
        String messageId = message.getMessageProperties().getMessageId();
        log.info("接收到消息，消息ID为：{}，消息内容为：{}", messageId, dataString);
        try {
            System.out.println(dataString);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 消息是否重复投递过
            Boolean redelivered = message.getMessageProperties().getRedelivered();
            log.error("消息ID为：{}，消息内容：{}，发生异常：{}, redelivered: {}", messageId, dataString, e.getMessage(), redelivered);
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, !redelivered);
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = QUEUE_NAME, durable = "true"),
            exchange = @Exchange(value = EXCHANGE_DIRECT),
            key = {ROUTING_KEY}
    ))
    public void processMessageTestPrefetch(String dataString, Message message, Channel channel) throws IOException {
        String messageId = message.getMessageProperties().getMessageId();
        try {
            TimeUnit.SECONDS.sleep(1);
            System.out.println(dataString);
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 消息是否重复投递过
            Boolean redelivered = message.getMessageProperties().getRedelivered();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, !redelivered);
        }
    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = NORMAL_QUEUE_NAME, durable = "true"),
//            exchange = @Exchange(value = NORMAL_EXCHANGE_DIRECT),
//            key = {NORMAL_ROUTING_KEY}
//    ))
    @RabbitListener(queues = {NORMAL_QUEUE_NAME})
    public void processMessageNormal(Message message, Channel channel) throws IOException {
        log.info("*[normal]消息接收到, 但我拒绝");
        channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = DEAD_QUEUE_NAME, durable = "true"),
//            exchange = @Exchange(value = DEAD_EXCHANGE_DIRECT),
//            key = {DEAD_ROUTING_KEY}
//    ))
    @RabbitListener(queues = {DEAD_QUEUE_NAME})
    public void processMessageDead(String dataString, Message message, Channel channel) throws IOException {
        log.info("*[dead letter]dataString = {}", dataString);
        log.info("*[dead letter]我是死信监听方法, 我接收到了死信消息");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
